<?php


namespace app\admin\controller\business;


use app\admin\model\business\Business as BusinessModel;
use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Business extends Adminbase
{
    protected $searchFields = 'business_name';

    protected function initialize()
    {
        $this->modelClass = new BusinessModel;
        parent::initialize();
        $this->admin1 = Session::get("admin");
    }
    public function index()
    {
        $status = input('param.status/d');
        if ($this->request->isAjax()) {
            $where2 = array();
            //todo is_del管理员登录状态修改 1 roleid 超级管理员 3分公司 4运营中心
            if ($this->admin1['roleid'] == 3) {
                if ($status == 0) {
                    //
                    $area_name=Db::name('area')->find($this->admin1['area_id']);
                    $where2[]=['city','=',$area_name['name']];
                } else {
                    $admin_where = array(
                        'is_del' => 0,
                        'roleid' => 4,
                        'area_id' => $this->admin1['area_id']
                    );
                    $belong_admin_ids = Db::name('admin')->where($admin_where)->column('id');
                    $where2[] = ['belong_admin_id', 'in', $belong_admin_ids];
                }
            }
            if ($this->admin1['roleid'] == 4) {
                $where2[] = ['belong_admin_id', '=', $this->admin1['id']];
            }
            if (isset($status)) {
                $where2[] = ['status', '=', $status];
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = $this->modelClass->where($where)->where($where2)->page($page, $limit)->select();
            $total = $this->modelClass->where($where)->where($where2)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        if (isset($status)) {
            $this->assign('status', $status);
        }
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            //获取管理员列表
            $business_id = $this->request->param('id/d', 0);
            $params = $this->request->post("row/a");
            $business_info = BusinessModel::get($business_id);
            if (empty($business_info)) {
                $this->error('服务站不存在！');
            }
            if (false === $this->modelClass->save([
                    'belong_admin_id' => $params['belong_admin_id'],
                    'yingye_name' => $params['yingye_name'],
                    'bank_name' => $params['bank_name'],
                    'bank_no' => $params['bank_no'],
                    'yingye_address' => $params['yingye_address'],
                    'receive_email' => $params['receive_email'],
                    'status' => $params['status']
                ], ['id' => $params['id']])) {
                $this->error('更新失败！');
            }
            $this->success("更新成功！", url("index"));
        } else {
            $userid = $this->request->param('id/d', 0);
            $data = $this->modelClass->get($userid);
            if (empty($data)) {
                $this->error("该会员不存在！");
            }
            //todo 超级管理员不设权限 roleid 3 分公司 显示自己所属的运营部门
            $where[] = ['roleid', '=', 4];
            $where[] = ['is_del', '=', 0];
            $where[] = ['status', '=', 1];
            if ($this->admin1['roleid'] == 3) {
                $where[]=['area_id','=',$this->admin1['area_id']];
            }
            $admin_list = Db::name('admin')
                ->where($where)
                ->order('create_time desc')
                ->field('id,username,create_time,nickname')
                ->select();
            $this->assign("data", $data);
            $this->assign("admin_list", $admin_list);
            //获取服务站管理员列表
            return $this->fetch();
        }
    }
}